package loadData

import (
	"fmt"
	cp "db2s/componentInit"
	"db2s/global"
	mq "db2s/topic-mq"
)
import "sync/atomic"

func subTaskInit(plan2 *SchedulePlan2) {
	var (
		selectCount int64 = 0
		insertCount int64 = 0
		recordCount int64 = 0
		taskStatus  int64 = 1
		beginTime   int64 = 0
		errinfoInit atomic.Value
		tableObject string
		indexObject string
		c           = global.GetIoc().GetBean("globalConfig").(*GlobalParametersConfig)
	)
	errinfoInit.Store("")
	switch plan2.Object.PartitionData.Logo.Source {
	case "partition":
		tableObject = "P"
	case "single":
		tableObject = "N"
	}
	switch len(plan2.ReadOptimizer.IndexColumn) {
	case 0:
		indexObject = "M"
	default:
		indexObject = "I"
	}

	plan2.subTaskInfo = &mq.SubTasks{
		Schema:      plan2.Object.Schema,
		Table:       plan2.Object.Table,
		Loc:         plan2.TableLocNumber,
		SAccumulate: &selectCount,
		RecordCount: &recordCount,
		DAccumulate: &insertCount,
		TaskStatus:  &taskStatus,
		ErrorInfo:   errinfoInit,
		BeginTime:   &beginTime,
		TableObject: tableObject,
		TopicCount: func() (tc map[string]*int64) {
			tc = make(map[string]*int64)
			for _, v1 := range GetCheckToTopicMQListeners()[cp.CheckMode(c.rules.CheckMode)] {
				var ff int64 = 0
				tc[string(v1.TopicName)] = &ff
			}
			return
		}(),
	}
	plan2.subTaskInfo.IndexName.Store(func() string {
		if len(plan2.ReadOptimizer.IndexName) == 0 {
			return "no"
		}
		return plan2.ReadOptimizer.IndexName
	}())
	plan2.subTaskInfo.IndexCardinality.Store(func() string {
		if len(plan2.ReadOptimizer.IndexCardinality) == 0 {
			return "0"
		}
		return plan2.ReadOptimizer.IndexCardinality
	}())
	plan2.subTaskInfo.IndexObject.Store(indexObject)
	fmt.Println("---kkk:", plan2.subTaskInfo.IndexName, plan2.subTaskInfo.IndexCardinality)
	PlanContext.TaskBarSubsStatusSend(plan2.subTaskInfo)
}

func (context *SchedulePlanContext) SubTaskBarAccumulate(n any) {
	context.TaskBarTable.BarAccumulate(n)
}
func (context *SchedulePlanContext) SpeedTaskBarAccumulate(n any) {
	context.TaskBarTable.SpeedAccumulate(n)
}
func (context *SchedulePlanContext) taskBarEndTopicAdd(n any) {
	context.TaskBarTable.TopicBarAdd(n)
}
func (context *SchedulePlanContext) TaskBarEndTopicDel(n any) {
	context.TaskBarTable.TopicBarDel(n)
}
func (context *SchedulePlanContext) TaskBarSubsStatusSend(table *mq.SubTasks) {
	context.TaskBarTable.BarSubsStatusSwapSend(table)
}
func (context *SchedulePlanContext) TaskBarSubsStatusSwap(table *mq.SubTasks, r string) {
	context.TaskBarTable.BarSubsStatusSwap(table, r)
}
func (context *SchedulePlanContext) TaskBarSubsErrorSwap(table *mq.SubTasks, r string) {
	context.TaskBarTable.BarSubsErrorSwap(table, r)
}
func (context *SchedulePlanContext) TaskBarSubsStatusGet(table *mq.SubTasks) int64 {
	return context.TaskBarTable.BarSubsStatusGet(table)
}
func (context *SchedulePlanContext) TpodTableStatusSwap(status *int64, r string) {
	switch r {
	case "error":
		atomic.SwapInt64(status, -1)
	case "abnormal":
		atomic.SwapInt64(status, -2)
	case "finish":
		atomic.SwapInt64(status, 0)
	}
}
